<template>
  <li @contextmenu.prevent.stop="f($event)">
    <!-- 选择框部分 -->
    <div class="check">
      <label class="cl-checkbox">
        <input
          type="checkbox"
          @change="handleClick(todo.id)"
          :checked="todo.done"
        />
        <span></span>
      </label>
      <!-- //Todo的事部分 -->
      <div class="title">
        <span v-show="!todo.isEdit"> {{ todo.title }} </span>
        <input
          type="text"
          id="changeInput"
          :value="todo.title"
          v-show="todo.isEdit"
          @blur="changeBlur(todo,$event)"
          ref="inputTitle"
          @keyup.enter="changeBlur(todo,$event)"
        />
      </div>
    </div>

    <!-- //删除按钮部分 -->
    <div class="buttons">
      <button @click="deleteAllElement(todo.id)">删除</button>
    </div>
  </li>
</template>

<script>
export default {
  name: "TodoListItem",
  methods: {
    f(e) {
      // console.dir(this.$el);
      this.$bus.$emit("showWindow", e, this.todo);
      this.$bus.$emit("sendData", this.todo);
    },
    deleteAllElement(id) {
      // this.removeObj(id)
      this.$bus.$emit("delete", id);
    },
    handleClick(v) {
      // console.log(v);
      this.$bus.$emit("check", v);
    },
    isEdit(data) {
      this.$set(data, "isEdit", true);
      this.$nextTick(()=>{
        this.$refs.inputTitle.focus();
        this.$refs.inputTitle.select();
      })
    },
    changeBlur(todo,e) {
      this.$set(todo, "title", e.target.value);
      this.$set(todo, "isEdit", false);
    },
  },
  props: ["todo"],
  mounted() {
    this.$bus.$on("edit", this.isEdit);
  },
};
</script>

<style scoped lang="scss">
/* Hide the default checkbox */
li {
  cursor: default;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 50px;
  /* background-color: #255cd2; */
  /* margin-top: 4px; */
  border-bottom: 1px #eee solid;
  position: relative;
}
li:nth-child(1) {
  margin-top: 10px;
}
li:hover {
  background-color: #eee;
}
.check {
  display: flex;
}
.title {
  margin-left: 10px;
  float: right;
  color: #000;
}
.title #changeInput {
  outline: none;
  /* width: 100%; */
  /* height: 8%; */
  border: none;
  border: 1px solid rgba(0, 0, 0, 0.2);
  border-radius: 3px;
  transition: 0.3s;
  padding: 5px 5px;
  font-size: 16px;
  &:hover {
    border: 1px solid rgba($color: #000000, $alpha: 0.7);
  }
}
.cl-checkbox {
  position: relative;
  display: inline-block;
}

/* Input */
.cl-checkbox > input {
  appearance: none;
  -moz-appearance: none;
  -webkit-appearance: none;
  z-index: -1;
  position: absolute;
  left: -10px;
  top: -8px;
  display: block;
  margin: 0;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  background-color: rgba(0, 0, 0, 0.6);
  box-shadow: none;
  outline: none;
  opacity: 0;
  transform: scale(1);
  pointer-events: none;
  transition: opacity 0.3s, transform 0.2s;
}

/* Span */
.cl-checkbox > span {
  display: inline-block;
  width: 100%;
  cursor: pointer;
}

/* Box */
.cl-checkbox > span::before {
  content: "";
  display: inline-block;
  box-sizing: border-box;
  margin: 3px 11px 3px 1px;
  border: solid 2px;
  /* Safari */
  border-color: rgba(0, 0, 0, 0.6);
  border-radius: 2px;
  width: 18px;
  height: 18px;
  vertical-align: top;
  transition: border-color 0.2s, background-color 0.2s;
}

/* Checkmark */
.cl-checkbox > span::after {
  content: "";
  display: block;
  position: absolute;
  top: 3px;
  left: 1px;
  width: 10px;
  height: 5px;
  border: solid 2px transparent;
  border-right: none;
  border-top: none;
  transform: translate(3px, 4px) rotate(-45deg);
}

/* Checked, Indeterminate */
.cl-checkbox > input:checked,
.cl-checkbox > input:indeterminate {
  background-color: #018786;
}

.cl-checkbox > input:checked + span::before,
.cl-checkbox > input:indeterminate + span::before {
  border-color: #018786;
  background-color: #018786;
}

.cl-checkbox > input:checked + span::after,
.cl-checkbox > input:indeterminate + span::after {
  border-color: #fff;
}

.cl-checkbox > input:indeterminate + span::after {
  border-left: none;
  transform: translate(4px, 3px);
}

/* Hover, Focus */
.cl-checkbox:hover > input {
  opacity: 0.04;
}

.cl-checkbox > input:focus {
  opacity: 0.12;
}

.cl-checkbox:hover > input:focus {
  opacity: 0.16;
}

/* Active */
.cl-checkbox > input:active {
  opacity: 1;
  transform: scale(0);
  transition: transform 0s, opacity 0s;
}

.cl-checkbox > input:active + span::before {
  border-color: #85b8b7;
}

.cl-checkbox > input:checked:active + span::before {
  border-color: transparent;
  background-color: rgba(0, 0, 0, 0.6);
}

/* Disabled */
.cl-checkbox > input:disabled {
  opacity: 0;
}

.cl-checkbox > input:disabled + span {
  color: rgba(0, 0, 0, 0.38);
  cursor: initial;
}

.cl-checkbox > input:disabled + span::before {
  border-color: currentColor;
}

.cl-checkbox > input:checked:disabled + span::before,
.cl-checkbox > input:indeterminate:disabled + span::before {
  border-color: transparent;
  background-color: currentColor;
}
.buttons {
  margin-right: 5px;
}
button {
  width: 70px;
  height: 35px;
  outline: none;
  background-color: rgba(255, 0, 0, 0.8);
  border: 1px solid transparent;
  color: white;
  white-space: nowrap;
  text-align: center;
  font-size: 14px;
  border-radius: 3px;
  cursor: pointer;
  transition: 0.3s;
}
button:hover {
  background-color: rgba(255, 0, 0, 0.4);
}
</style>